import {LabelSort} from "@/base/model/LabelSort";
import {Page} from "@/base/Page";
import {Company} from "@/base/model/Company";
import {DeviceModel} from "@/base/model/DeviceModel";
import {Label} from "@/base/model/Label";
import {getLabelList,getLabelSort} from "@/base/api/LabelApi";
import {getCompanyList} from "@/base/api/CompanyApi";
import {deepAssign} from "@/base/util/Objects";
import {deleteLabelSort,putLabelSortSort,postLabelSort,putLabelSort,deleteLabel} from "@/base/api/LabelApi";
export class LabelBloc {
  // device: any = [];
  currentIdx: number = 0;
  sortLabel = new LabelSort();
  // modelPut: any = {
  //  labelsortID = 0;
  //  labelsortname = '';
  //  sort = '';
  // };//labelsort
  //
  model: any = {
    labelId : 0,
    labelname : '',
    companyId : null,
    // updatetime = '',
    // sort = 0,
    labelsortId : 0,
  };// label

  modelPut: any={
    labelsortId : 0,
  };
  sortPage: Page<LabelSort> = new Page<LabelSort>([], 0, 1, 10, 0, []);
  company: Page<Company> = new Page<Company>([], 0, 1, 10, 0, []);
  LabelPage: Page<Label> = new Page<Label>([], 0, 1, 15, 0, []);
  // devices: Page<Label> = new Page<Label>([], 0, 1, 10, 0, []);
  loading = false;
  sortList: any = [];
  num: number = 0;
  companyList: any = [];

  // load(model) {
  //   this.loading = true;
  //   let that = this;
  //   this.num++;
  //   return getLabelList({
  //     pageIndex: 1,
  //     pageSize: 15,
  //     sortDirection: "DESC",
  //
  //   }).then((res) => {
  //     this.model = res.data
  //     return res;
  //   }).finally(() => {
  //     this.loading = false;
  //   });
  // }
  //
  getCompanyList(pageIndex: number, pageSize: number, sortDirection: string) {
    return getCompanyList({ pageIndex, pageSize, sortDirection, name: '', createBegin: '' })
      .then((res) => {
        deepAssign(this.company, res.data);

        this.companyList = res.data.items.map(item => {
          let obj = { companyId: 0, companyName: '' };
          obj.companyId = item.id;
          obj.companyName = item.name;
          return obj
        });
        return res;
      }).finally(() => {
        this.loading = false;
      });
  }
  //
  getCompanyName(companyId: number) {
    const item = this.companyList.find(x => x.companyId === companyId);
    return item == null ? '' : item.companyName;
  }
  //
  addSort(labelsortname: string) {
    this.loading = true;
    return postLabelSort({ labelsortname }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
  //
  editSort(labelsortId: number, labelsortname: string) {
    this.loading = true;
    return putLabelSort({ labelsortId, labelsortname }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
  //
  deleteSort(labelsortId: number) {
    this.loading = true;
    return deleteLabelSort({ labelsortId }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
  //
  putLabelSortSort(id: []) {
    this.loading = true;
    return putLabelSortSort({ id }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
  //
  // putDeviceModelSort(row, type) {
  //   // this.getDeviceModel(1,100,'ASC',this.model.sortId,this.model.companyId).then(res => {
  //   //
  //   // })
  //   let modelIdS = this.modelPage.items.map(item => {
  //     return item.id
  //   })
  //   let index = modelIdS.findIndex(modelId => {
  //     return modelId === row.id
  //   })
  //   if (type === 'down') {
  //     if (index < modelIdS.length - 1) {
  //       let modelId = modelIdS[index]
  //       modelIdS[index] = modelIdS[index + 1]
  //       modelIdS[index + 1] = modelId
  //     } else {
  //       let modelId = modelIdS[index]
  //       modelIdS[index] = modelIdS[0]
  //       modelIdS[0] = modelId
  //     }
  //   }
  //   if (type === 'up') {
  //     if (index > 0) {
  //       let modelId = modelIdS[index]
  //       modelIdS[index] = modelIdS[index - 1]
  //       modelIdS[index - 1] = modelId
  //     } else {
  //       let modelId = modelIdS[index]
  //       modelIdS[index] = modelIdS[modelIdS.length - 1]
  //       modelIdS[modelIdS.length - 1] = modelId
  //     }
  //   }
  //   this.model.sortId = row.sortId
  //   return putDeviceModelSort({ id: modelIdS }).then(res => {
  //     this.getDeviceModel(1, 100, 'ASC', this.model.sortId, this.model.companyId, this.model.modelName)
  //     return res
  //   })
  // }
  //

  getLabels
  (pageIndex: number,
   pageSize: number,
   sortDirection: string,
   labelsortId,
   companyId,
   labelname,
   )
  {
    this.loading = true;
    return getLabelList({
      pageIndex,
      pageSize,
      sortDirection,
      companyId,
      labelsortId,
      labelname: labelname,
    }).then((res) => {
      deepAssign(this.LabelPage, res.data);
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getAllSort(pageIndex: number,
             pageSize: number,
             sortDirection: string,
             labelsortname?:string,
             ) {
    this.loading = true;
    // let that = this;
    return getLabelSort({
      pageIndex,
      pageSize,
      sortDirection,
      labelsortname,
    }).then((res) => {
      this.sortPage = res.data;
      this.sortList = res.data.items.map(function (item) {
        var obj = { labelsortId: 0, labelsortname: '' };
        // @ts-ignore
        obj.labelsortId = item.labelsortId;
        obj.labelsortname = item.labelsortname;
        return obj;
      });
      // if (this.sortDevice.id === 0) {
      //   this.sortDevice = this.sortList[0]
      // }
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }
  //
  getsortName(sortId: number) {
    const item = this.sortList.find(x => x.labelsortId === sortId);
    return item == null ? '' : item.labelsortname;
  }
  //
  // switchStatus1(id, value, index) {
  //   this.getModel(id)
  //     .then(res => {
  //       if (value) {
  //         this.modelPut.isFirmware = true
  //       } else {
  //         this.modelPut.isFirmware = false
  //       }
  //       this.editModel(this.modelPut)
  //     })
  // }
  //
  // switchStatus2(id, value, index) {
  //   this.getModel(id)
  //     .then(res => {
  //       if (value) {
  //         this.modelPut.isSelfMotion = true
  //       } else {
  //         this.modelPut.isSelfMotion = false
  //       }
  //       this.editModel(this.modelPut)
  //     })
  // }
  //
  // switchStatus3(id, value, index) {
  //   this.getModel(id)
  //     .then(res => {
  //       if (value) {
  //         this.modelPut.isSoftware = true
  //       } else {
  //         this.modelPut.isSoftware = false
  //       }
  //       this.editModel(this.modelPut)
  //     })
  // }
  //
  // getModel(id: number) {
  //   this.loading = true;
  //   return getDeviceModelId({ id }).then((res) => {
  //     deepAssign(this.modelPut, res.data);
  //     return res;
  //   }).finally(() => {
  //     this.loading = false;
  //   });
  // }
  //
  // editModel(modelPut: any) {
  //   return putDeviceModel({
  //     id: modelPut.id,
  //     companyId: modelPut.companyId,
  //     isFirmware: modelPut.isFirmware,
  //     isSelfMotion: modelPut.isSelfMotion,
  //     isSoftware: modelPut.isSoftware,
  //     modelIcon: modelPut.modelIcon,
  //     modelName: modelPut.modelName,
  //     sortId: modelPut.sortId
  //   }).then((res) => {
  //     return res;
  //   }).finally(() => {
  //     this.loading = false;
  //   });
  // }
  //
  deleteModel(labelsortId) {
    return deleteLabel({ labelsortId }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
  //
  // getDevice(
  //   pageIndex: number, pageSize: number, sortDirection: string, model: any) {
  //   this.loading = true;
  //   return getDevice({
  //     pageIndex: pageIndex,
  //     pageSize: pageSize,
  //     sortDirection: sortDirection,
  //     agentId: this.model.agentId,
  //     cityId: this.model.cityId,
  //     companyId: this.model.companyId,
  //     createBegin: this.model.createBegin,
  //     createEnd: this.model.createEnd,
  //     deviceSn: this.model.deviceSn,
  //     modelId: this.model.modelId,
  //     modelName: this.model.modelName,
  //     provinceId: this.model.provinceId,
  //     sortId: this.model.sortId,
  //     statusCode: this.model.statusCode
  //   }).then((res) => {
  //     this.devices = res.data;
  //     return res;
  //   }).finally(() => {
  //     this.loading = false;
  //   });
  // }

}
